package com.blink.zookeeper.operate.auth;

import com.blink.zookeeper.ZooKeeperCreator;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

//删除节点的权限控制
public class AuthSample_Delete {

    final static String PATH = "/zk-book-auth_test";
    final static String PATH2 = "/zk-book-auth_test/child";
    final static String PATH3 = "/zk-book-auth_test/child/subchild";

    public static void main(String[] args) throws Exception {

        ZooKeeper zookeeper1 = ZooKeeperCreator.getZk("zookeeper1");
        zookeeper1.addAuthInfo("digest", "foo:true".getBytes());
//        zookeeper1.create(PATH, "init".getBytes(), Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
//        zookeeper1.create(PATH2, "init".getBytes(), Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
        zookeeper1.create(PATH3, "init".getBytes(), Ids.CREATOR_ALL_ACL, CreateMode.EPHEMERAL);
        try {
            ZooKeeper zookeeper2 = ZooKeeperCreator.getZk("zookeeper2");
            zookeeper2.delete(PATH3, -1);
        } catch (Exception e) {
            System.out.println("删除节点失败: " + e.getMessage());
        }

        ZooKeeper zookeeper3 = ZooKeeperCreator.getZk("zookeeper3");
        zookeeper3.addAuthInfo("digest", "foo:true".getBytes());
        zookeeper3.delete(PATH3, -1);
        System.out.println("成功删除节点：" + PATH3);

        ZooKeeper zookeeper4 = ZooKeeperCreator.getZk("zookeeper4");
//        zookeeper4.delete(PATH2, -1);
        zookeeper3.delete(PATH2, -1);
        System.out.println("成功删除节点：" + PATH2);
        zookeeper4.delete(PATH, -1);
        System.out.println("成功删除节点：" + PATH);
    }
}